/**
* @description: 725. 分隔链表
* @author hewei
* @date 2022/8/18 14:19
* @version 1.0
*/

public class SplitListToParts {

    public ListNode[] splitListToParts(ListNode head, int k) {
        int count = 0;
        ListNode temp = head;
        ListNode[] ans = new ListNode[k];
        while (temp != null) {
            ++count;
            temp = temp.next;
        }
        int num = count / k;
        int delta = count % k;
        int index = 0;
        int a = 1;
        temp = head;
        while (temp != null) {
            ListNode next = temp.next;
            if (a == 1) {
                ans[index++] = temp;
            }
            if ((a == num && delta == 0) || (a == num + 1 && delta != 0)) {
                a = 1;
                temp.next = null;
                if (delta > 0) --delta;
            } else ++a;
            temp = next;
        }
        return ans;
    }
}
